#!/bin/bash

# BellaTravel 性能优化脚本
# 用途：快速应用关键性能优化

set -e

echo "🚀 开始性能优化..."
echo ""

# 1. 检查 Hermes 是否启用
echo "📱 检查 Hermes 配置..."

if grep -q "hermesEnabled=true" android/gradle.properties; then
    echo "✅ Android Hermes 已启用"
else
    echo "⚠️  启用 Android Hermes..."
    echo "hermesEnabled=true" >> android/gradle.properties
fi

# 检查 iOS Podfile
if grep -q "hermes_enabled.*true" ios/Podfile; then
    echo "✅ iOS Hermes 已启用"
else
    echo "⚠️  提醒：请手动在 ios/Podfile 中设置 hermes_enabled: true"
fi

echo ""

# 2. 启用 Android 构建优化
echo "🔧 优化 Android Gradle 配置..."

GRADLE_PROPS="android/gradle.properties"

# 添加 Gradle 优化配置
if ! grep -q "org.gradle.caching=true" $GRADLE_PROPS; then
    echo "org.gradle.caching=true" >> $GRADLE_PROPS
    echo "org.gradle.parallel=true" >> $GRADLE_PROPS
    echo "org.gradle.configureondemand=true" >> $GRADLE_PROPS
    echo "android.enableR8.fullMode=true" >> $GRADLE_PROPS
    echo "✅ Android 构建优化已添加"
else
    echo "✅ Android 构建优化已存在"
fi

echo ""

# 3. 清理缓存
echo "🧹 清理缓存..."
rm -rf node_modules/.cache
rm -rf $TMPDIR/react-*
rm -rf $TMPDIR/metro-*
echo "✅ 缓存已清理"

echo ""

# 4. 检查 babel 配置
echo "🔍 检查 Babel 配置..."
if grep -q "transform-remove-console" babel.config.js; then
    echo "✅ Console 移除插件已配置"
else
    echo "⚠️  警告：未检测到 console 移除配置"
fi

echo ""

# 5. 统计当前 console.log 数量（用于对比）
echo "📊 统计代码中的 console 语句..."
CONSOLE_COUNT=$(find src -name "*.js" -o -name "*.ts" -o -name "*.tsx" | xargs grep -c "console\\.log" 2>/dev/null | awk -F: '{s+=$2} END {print s}')
echo "   发现 $CONSOLE_COUNT 处 console.log"
echo "   (生产打包时将自动移除)"

echo ""

# 6. 输出优化建议
echo "✨ 性能优化配置完成！"
echo ""
echo "📋 下一步建议："
echo "   1. 运行 'yarn clean:cache' 清理 Metro 缓存"
echo "   2. iOS: cd ios && pod install"
echo "   3. 测试 Release 包性能"
echo "   4. 考虑集成 react-native-fast-image"
echo ""
echo "📖 查看完整优化方案: PERFORMANCE_OPTIMIZATIONS.md"
